package com.medisign.sign.log.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.medisign.sign.log.config.SystemConfig;
import com.medisign.sign.log.constant.ActionEnum;
import com.medisign.sign.log.service.ISignService;
import com.medisign.sign.log.util.HttpClientUtil;
import com.medisign.sign.log.util.RandomUtil;
import com.medisign.sign.log.vo.GetTokenReqVo;
import com.medisign.sign.log.vo.P1SignReqVo;
import com.medisign.sign.log.vo.P1SignRespVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
@Service
public class SignServiceImpl implements ISignService {

    private final SystemConfig systemConfig;
    private final HttpClientUtil httpClientUtil;

    private String getToken() {
        GetTokenReqVo getTokenReqVo = new GetTokenReqVo();
        getTokenReqVo.setAppid(systemConfig.getSign().getAppid());
        getTokenReqVo.setAppkey(systemConfig.getSign().getAppkey());
        String getTokenReqJson = JSON.toJSONString(getTokenReqVo);
        String url = systemConfig.getSign().getHost() + ActionEnum.get_access_token.getValue();
        String getTokenResult = httpClientUtil.post(url, getTokenReqJson);
        if (StringUtils.isBlank(getTokenResult)) {
            log.error("获取token失败, 结果为空!");
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(getTokenResult);
        if (!"0".equals(jsonObject.getString("status"))) {
            log.error("获取token失败, 结果为:[{}]", getTokenResult);
            return null;
        }
        JSONObject dataJsonObject = jsonObject.getJSONObject("data");
        return dataJsonObject.getString("accessToken");
    }


    @Override
    public P1SignRespVo p1Sign(String data) {
        P1SignReqVo p1SignReqVo = P1SignReqVo.builder()
                .authKEY("000000")
                .userId(systemConfig.getSign().getUserId())
                .data(data)
                .isBackTimestamp("1")
                .transactionId(RandomUtil.getUUID())
                .isBackSignCert("1")
                .build();
        String token = getToken();
        if (StringUtils.isBlank(token)) {
            return new P1SignRespVo();
        }
        String p1ReqJson = JSON.toJSONString(p1SignReqVo);
        String url = systemConfig.getSign().getHost() + ActionEnum.p1_sign.getValue() + token;
        String p1RespResult = httpClientUtil.post(url, p1ReqJson);
        if (StringUtils.isBlank(p1RespResult)) {
            log.error("p1签名失败, 结果为空!");
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(p1RespResult);
        if (!"0".equals(jsonObject.getString("status"))) {
            log.error("p1失败, 结果为:[{}]", p1RespResult);
            return new P1SignRespVo();
        }
        String dataJSon = jsonObject.getString("data");
        return JSON.parseObject(dataJSon, P1SignRespVo.class);
    }
}
